var mouseDown = false;
var lastMouseX = null;
var lastMouseY = null;

var moveMouseX = 0;
var moveMouseY = 0;

var mouseButton = 0;

var moonRotationMatrix = Matrix.I(4);

var moonTranformation = -10;

var draw = 1;
var finishDrawing = 0;

function handleMouseDown(event) {
	mouseDown = true;
	mouseButton = event.button;
	lastMouseX = event.clientX;
	lastMouseY = event.clientY;
}

function handleMouseUp(event) {
	mouseDown = false;
}

function handleMouseMove(event) {
	if (!mouseDown) {
		return;
	}

	var newX = event.clientX;
	var newY = event.clientY;

	if (mouseButton == 0) //left button
	{
		var m = Matrix.Translation(
				$V([ moveMouseX, -moveMouseY, moonTranformation ])).ensure4x4();

		var deltaX = newX - lastMouseX;
		var newRotationMatrix = createRotationMatrix(-deltaX / 2, [ 0, 1, 0 ]);

		var deltaY = newY - lastMouseY;
		newRotationMatrix = newRotationMatrix.x(createRotationMatrix(
				deltaY / 2, [ 1, 0, 0 ]));

		moonRotationMatrix = newRotationMatrix.x(moonRotationMatrix);

		//moonRotationMatrix = newRotationMatrix.x(m);

		lastMouseX = newX;
		lastMouseY = newY;
	} else if (mouseButton == 2) //right
	{
		var deltaX = newX - lastMouseX;

		var deltaY = newY - lastMouseY;

		moveMouseX += (deltaX * (0.001736 * Math.abs(moonTranformation)));
		moveMouseY += (deltaY * (0.001736 * Math.abs(moonTranformation)));

		lastMouseX = newX;
		lastMouseY = newY;

	}

	if (finishDrawing == 1) {
		setTimeout(function() {
			drawScene(gl, shaderProgram, table);
		}, 15);
	} else {
		draw = 1;
	}
}

function wheel(event) {
	var delta = 0;
	if (!event) /* For IE. */
		event = window.event;
	if (event.wheelDelta) { /* IE/Opera. */
		delta = event.wheelDelta / 120;
		/** In Opera 9, delta differs in sign as compared to IE.
		 */
		if (window.opera)
			delta = -delta;
	} else if (event.detail) {
		/** Mozilla case. */
		/** In Mozilla, sign of delta is different than in IE.
		 * Also, delta is multiple of 3.
		 */
		delta = -event.detail / 3;
	}
	/** If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */
	if (delta)
		handle(delta);
	/** Prevent default actions caused by mouse wheel.
	 * That might be ugly, but we handle scrolls somehow
	 * anyway, so don't bother here..
	 */
	if (event.preventDefault)
		event.preventDefault();
	event.returnValue = false;
}

function handle(delta) {
	//alert(delta);
	// if (delta != 0)
	moonTranformation = delta + moonTranformation;

	if (finishDrawing == 1) {
		setTimeout(function() {
			drawScene(gl, shaderProgram, table);
		}, 15);
	} else {
		draw = 1;
	}
	//	 alert(moonTranformation);
}

//block mouse

message = "";

function clickIE() {
	if (document.all) {
		(message);
		return false;
	}
}
function clickNS(e) {
	if (document.layers || (document.getElementById && !document.all)) {
		if (e.which == 2 || e.which == 3) {
			(message);
			return false;
		}
	}
}
if (document.layers) {
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown = clickNS;
} else {
	document.onmouseup = clickNS;
	document.oncontextmenu = clickIE;
}

document.oncontextmenu = new Function("return false");